using System;
using System.Security.Cryptography;
using System.Text;
using Castle.ActiveRecord;
using CosyTrade.Data;

namespace CosyTrade.Security
{
    [AddPropertyChangedNotifyInterface, PropertyChangedNotify]
    [Serializable, ActiveRecord("Users")]    
    public class User : BusinessObject
    {
        private readonly Guid primaryKey = Guid.NewGuid();
        private string userName;
        private string password;
        private Role mapToRole;
        private bool enable;
        
        [PrimaryKey(PrimaryKeyType.Assigned, "ROWGUID", Access=PropertyAccess.NosetterCamelcase)]
        public Guid PrimaryKey
        {
            get { return primaryKey; }
        }

        [Property]
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        [Property("ePassword")]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        [BelongsTo("RoleGuid", NotNull=true)]
        public Role MapToRole
        {
            get { return mapToRole; }
            set { mapToRole = value; }
        }

        [Property]
        public bool Enable
        {
            get { return enable; }
            set { enable = value; }
        }

        public static string Encode(string code)
        {
            MD5 md5Encoder = new MD5CryptoServiceProvider();
            byte[] encode = md5Encoder.ComputeHash(Encoding.UTF8.GetBytes(code));
            return BitConverter.ToString(encode);
        }

    }
}